package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.AdministratorService;
import com.igeekhome.biz.CustomerInfoService;
import com.igeekhome.biz.CustomerServiceService;
import com.igeekhome.biz.NoticeService;
import com.igeekhome.dao.CustomerServiceMapper;
import com.igeekhome.pojo.Administrator;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.Notice;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 客服表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/customerService")
public class CustomerServiceController {

    @Autowired
    private CustomerServiceService customerServiceService;
    @Autowired
    private CustomerInfoService customerInfoService;
    @Autowired
    private CustomerServiceMapper customerServiceMapper;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private  AdministratorService administratorService;

    @RequestMapping("/login")
    public String checkCustomerServiceNameAndPwd(CustomerService customerService, Model model, HttpSession session){
        String path = null;
        String master = null;
        QueryWrapper<CustomerService> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",customerService.getEmail());
        queryWrapper.eq("password",customerService.getPassword());
        CustomerService cs = customerServiceService.getOne(queryWrapper);  //获取登录客服人员的信息

        QueryWrapper<Administrator> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("email",customerService.getEmail());
        queryWrapper1.eq("password",customerService.getPassword());
        Administrator administrator= administratorService.getOne(queryWrapper1);  //获取管理员的信息
        if(administrator != null){  //将管理员是否存在信息的判断放在session里
            master = "true";
            session.setAttribute("master",master);
        }else{
            master = "false";
            session.setAttribute("master",master);
        }


        if(cs==null){

            path = "redirect:/login";//forward从当前路径转发，一次请求
            session.setAttribute("msg","用户名或密码不正确");
            System.out.println("用户名密码错误");

        }else{
            //首页4个大数据的显示
            int title1=customerServiceMapper.sessioncount(null);
            model.addAttribute("title1",title1);
            int title2=customerServiceMapper.session_processing(null);
            model.addAttribute("title2",title2);
            int title3=customerServiceMapper.cs_onlinecount(null);
            model.addAttribute("title3",title3);
            int title4=customerServiceMapper.totalMessageCount(null);
            model.addAttribute("title4",title4);

            //首页表(Notice)的插入及分页
            IPage<Notice> page=new Page<>(1,10);
            IPage<Notice> page1 = this.noticeService.page(page);
            List<Notice> list = page1.getRecords();
            long pagesCount = page1.getPages();
            model.addAttribute("list",list);
            model.addAttribute("pagesCount",pagesCount);

            //登录正确的跳转
            session.setAttribute("cs",cs);
            path = "forward:/index";//redirect从跟目录转发，二次请求
            System.out.println("正确");
        }

        return path;
    }
    @RequestMapping("/index")
    public String index2(Model model)
    {
        //首页4个大数据的显示
        int title1=customerServiceMapper.sessioncount(null);
        model.addAttribute("title1",title1);
        int title2=customerServiceMapper.session_processing(null);
        model.addAttribute("title2",title2);
        int title3=customerServiceMapper.cs_onlinecount(null);
        model.addAttribute("title3",title3);
        int title4=customerServiceMapper.totalMessageCount(null);
        model.addAttribute("title4",title4);

        //首页表(Notice)的插入及分页
        IPage<Notice> page=new Page<>(1,10);
        IPage<Notice> page1 = this.noticeService.page(page);
        List<Notice> list = page1.getRecords();
        long pagesCount = page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);

        return  "forward:/index";
    }

    //将注册账号数据传输到数据库，然后跳回到登录页面(单个账号)
    @RequestMapping("/register")
    public String registerCustomerService(CustomerService customerService, Model model, HttpSession session)
    {
       int result = customerServiceMapper.insert(customerService);
       return "login";
    }


    //客服信息管理
    @RequestMapping("/message")
    public String index(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        initcustomerservice(model,current,size);
        model.addAttribute("main","客服管理主页");
        return "customerservice";
    }


    //客服修改，同时也是设置中的个人信息设置
    //http://localhost/customerService/update?customerserviceid=1
    @RequestMapping("/update")
    public String update(Model model,CustomerService customerService,HttpSession saa)
    {
        model.addAttribute("main","个人信息修改");
        return  "updatecustomerservice";
    }

//更新前端收到数据，弹出一个提示窗口，并停留在本页
    @RequestMapping("/updateVerify")
    public String updateVerify(Model model,CustomerService customerService,HttpSession session)
    {
//        客服信息修改方式，可以运行，但选择下面的方式较为简单
//        CustomerService   cs = (CustomerService) session.getAttribute("cs");
//        cs.setNickname(customerService.getNickname());
//        this.customerServiceService.updateById(cs);
//        session.setAttribute("cs",cs);
        this.customerServiceService.updateById(customerService);
        session.setAttribute("cs",customerService);
        model.addAttribute("main","个人信息修改");

       // model.addAttribute("msg","<script>alert(1)</script>");
        return  "updatecustomerservice";

    }


    //创建新客服
    @RequestMapping("/add")
    public String add(Model model,CustomerService customerService){
        model.addAttribute("main","创建新客服");
        return "addcustomerservice";
    }

    @RequestMapping("/addVerify")
    public String addVerify(Model model,CustomerService customerService, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        customerServiceMapper.insert(customerService);
        model.addAttribute("main","客服管理主页");
        model.addAttribute("current",current);
        model.addAttribute("size",size);
        initcustomerservice(model,current,size);
        return "customerservice";
    }

    //得到客服列表中的要修改的对象信息，然后跳转到修改页面
    @RequestMapping("/updateAll")
    public String update(Model model, CustomerService customerService, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        CustomerService customerService1= customerServiceService.getById(customerService.getCustomerserviceid());
        model.addAttribute("cs",customerService1);
        model.addAttribute("current",current);
        model.addAttribute("size",size);
        model.addAttribute("main","客服人员修改");
        return "updateallcustomerservice";
    }


    //修改完成,并跳转到客服管理界面
    @RequestMapping("/updateAllVerify")
    public String updateVerify(CustomerService customerService, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size){

        this.customerServiceService.updateById(customerService);
        initcustomerservice(model,current,size);
        model.addAttribute("main","客服管理主页");
        return "customerservice";

    }

//    删除客服
    @RequestMapping("/del")
    public String del(HttpSession session,Model model,CustomerService customerService,@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        String administrator =  (String)session.getAttribute("master");
        //只有管理员才能修改
        if(administrator.equals("true")) {
            this.customerServiceService.removeById(customerService.getCustomerserviceid());
        }
        initcustomerservice(model,current,size);
        return "customerservice";
    }


    public void initcustomerservice(Model model, Integer current, Integer size)
    {

        IPage<CustomerService> page=new Page<>(current,size);
        IPage<CustomerService> page1 = this.customerServiceService.page(page);
        List<CustomerService> list = page1.getRecords();
        long pagesCount = page1.getPages();

        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
    }
}

